package com.letou.ls.view;

import java.util.ArrayList;

import com.letou.ls.uitl.Logger;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

public class TableBadyLineView extends View {
    
    private ArrayList<ArrayList<ArrayList<String>>> mDataList;
    private int tableTextColor = Color.BLACK;
    private int tableBackgroundAColor = Color.WHITE;
    private int tableBackgroundCColor = Color.parseColor("#ccffff");
    private int tableBackgroundDColor = Color.parseColor("#cacdd5");
    private ArrayList<Float> mWidthList;
    private int[] redLine = {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 1, 0, 0, 0, 1};
    
    /**
     * 位图变量。
     */
    public Bitmap mBitmap;
    /**
     * 画布变量。
     */
    // private Canvas mCanvas;
    /**
     * 图片控制矩阵。
     */
    private Matrix mMatrix = new Matrix();
    public float mWidth;
    public float mHeight;
    private Paint mPaint;
    private Paint mBgPaint;
    private float textSize;
    private float itemHeight;
    private ArrayList<Integer> lineOne;
    private ArrayList<Integer> lineTwo;
    private ArrayList<Integer> lineThree;
    private int startOne;
    private int startTwo;
    private int startThree;
    private Paint mLinePaint;
    
    public TableBadyLineView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mPaint = new Paint();
        /* 去锯齿 */
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        
        mBgPaint = new Paint();
        /* 去锯齿 */
        mBgPaint.setAntiAlias(true);
        mBgPaint.setColor(Color.BLACK);
        mBgPaint.setStrokeWidth(2);
        
        mLinePaint = new Paint();
        /* 去锯齿 */
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStrokeWidth(1);
        mLinePaint.setColor(Color.BLUE);
        
    }
    
    public TableBadyLineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    
    public TableBadyLineView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
    }
    
    public void startLine(int startOne, int startTwo, int startThree) {
        this.startOne = startOne;
        this.startTwo = startTwo;
        this.startThree = startThree;
    }
    
    public void addLine(ArrayList<Integer> oneLine, ArrayList<Integer> twoLine,
            ArrayList<Integer> threeLine, int[] redLine) {
        lineOne = oneLine;
        lineTwo = twoLine;
        lineThree = threeLine;
        this.redLine = redLine;
        
        if (oneLine == null) {
            Logger.v("main", "oneLine==null");
        } else {
            Logger.v("main", "oneLine " + oneLine.size());
        }
    }
    
    public void updateData(ArrayList<ArrayList<ArrayList<String>>> datalist,
            ArrayList<Float> widthList, float textSize, float itemHeight) {
        this.setBackgroundColor(Color.parseColor("#3962b4"));
        
        mDataList = datalist;
        mWidthList = widthList;
        mPaint.setTextSize(textSize);
        this.textSize = textSize;
        this.itemHeight = itemHeight;
        reDarwBitmap();
        invalidate();
    }
    
    public ArrayList<ArrayList<ArrayList<String>>> get_data() {
        return mDataList;
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        
        mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
        mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension((int) mWidth, (int) mHeight);
        
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Logger.v("main", "onDraw   " + mWidth + "  " + mHeight);
        if (mBitmap == null) {
            reDarwBitmap();
        }
        if (mBitmap != null) {
            canvas.drawBitmap(mBitmap, mMatrix, null);
        }
    }
    
    private void reDarwBitmap() {
        if (mBitmap != null) {
            mBitmap.recycle();
        }
        if (mHeight <= 0) {
            return;
        }
        mBitmap = Bitmap.createBitmap((int) mWidth, (int) mHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mBitmap);
        canvas.drawBitmap(mBitmap, mMatrix, null);
        
        /**
         * 画表内容01-14 15:28:07.044: V/lss(19466):
         * http://119.10.9.19:8080/le-tou/api/account/login?username=customer01&
         * password=customer01&mac=00-01-87-af-0d
         *
         */
        if (mDataList != null) {
            int size = mDataList.size();
            float startY = 1;
            float[] widthArray = null;
            for (int i = 0; i < size; i++) {
                int ss = mDataList.get(i).size();
                float startX = 0;
                
                if (widthArray == null) {
                    widthArray = new float[ss];
                }
                for (int j = 0; j < ss; j++) {
                    float width;
                    if (j < mWidthList.size()) {
                        width = mWidthList.get(j).intValue();
                    } else {
                        width = 50;
                    }
                    
                    String type = null;
                    if (mDataList.get(i).get(j).size() == 2) {
                        type = mDataList.get(i).get(j).get(1);
                    }
                    
                    if ("1".equals(type)) {
                        mBgPaint.setColor(tableBackgroundAColor);
                    } else if ("2".equals(type)) {
                        
                        mBgPaint.setColor(tableBackgroundCColor);
                    } else if ("3".equals(type)) {
                        
                        mBgPaint.setColor(tableBackgroundCColor);
                    } else if ("4".equals(type)) {
                        
                        mBgPaint.setColor(tableBackgroundDColor);
                    } else if ("5".equals(type)) {
                        
                        mBgPaint.setColor(tableBackgroundDColor);
                    } else {
                        mBgPaint.setColor(tableBackgroundAColor);
                    }
                    if (width != 0) {
                        float x = startX;
                        float y = startY;
                        startX += width + 1;
                        if (i == 0) {
                            widthArray[j] = startX;
                        }
                        
                        canvas.drawRect(x, y, x + width - 1, y + itemHeight, mBgPaint);
                    }


                }
                
                if (i != 0) {
                    drawLine(i, startOne, startY, lineOne, widthArray, canvas);
                    drawLine(i, startTwo, startY, lineTwo, widthArray, canvas);
                    drawLine(i, startThree, startY, lineThree, widthArray, canvas);
                }
                startY += itemHeight + 1;
            }
            
            startY = 1;
            for (int i = 0; i < size; i++) {
                int ss = mDataList.get(i).size();
                float startX = 0;
                for (int j = 0; j < ss; j++) {
                    String label = mDataList.get(i).get(j).get(0);
                    float width;
                    if (j < mWidthList.size()) {
                        width = mWidthList.get(j).intValue();
                    } else {
                        width = 50;
                    }
                    
                    String type = null;
                    if (mDataList.get(i).get(j).size() == 2) {
                        type = mDataList.get(i).get(j).get(1);
                    }
                    if ("1".equals(type)) {
                        mPaint.setColor(Color.RED);
                    } else if ("2".equals(type)) {
                        mPaint.setColor(Color.RED);
                    } else if ("3".equals(type)) {
                        mPaint.setColor(tableTextColor);
                    } else if ("4".equals(type)) {
                        mPaint.setColor(tableTextColor);
                    } else if ("5".equals(type)) {
                        mPaint.setColor(Color.RED);
                    } else {
                        mPaint.setColor(tableTextColor);
                    }
                    if (width != 0) {
                        float x = startX;
                        float y = startY;
                        startX += width + 1;
                        
                        if (!TextUtils.isEmpty(label)) {
                            float textWidth = mPaint.measureText(label);
                            x += (width - textWidth) / 2f;
                            canvas.drawText(label, x - 1, y + (textSize + itemHeight - 4) / 2f,
                                    mPaint);
                            Typeface font = Typeface.create(Typeface.DEFAULT, Typeface.BOLD);
                            mPaint.setTypeface(font);
                        }
                    }
                    
                    
                }
                // if (i % 10 == 0 && i != 0) {
                // mBgPaint.setColor(Color.parseColor("#FF7F00"));
                // float y = startY;
                // canvas.drawLine(0, y, mWidth, y, mBgPaint);
                // }
                startY += itemHeight + 1;
            }
        }
        if (mWidthList == null || mWidthList.isEmpty()) {
            return;
        }
        int size = mWidthList.size();
        float bgHeight = mDataList.size() * (itemHeight + 1) + 1;
        int startX = 0;
        mBgPaint.setStrokeWidth(2);
        for (int index = 0; index < size; index++) {
            
            float width = mWidthList.get(index).intValue();
            startX += width + 1;
            int x = startX;
            if (index < redLine.length) {
                switch (redLine[index]) {
                    case 0:
                        // mBgPaint.setColor(Color.WHITE);
                        // canvas.drawLine(x, 0, x, bgHeight, mBgPaint);
                        break;
                    case 1:
                        mBgPaint.setColor(Color.RED);
                        canvas.drawLine(x, 0, x, bgHeight, mBgPaint);
                        break;
                    case 2:
                        mBgPaint.setColor(Color.parseColor("#FF7F00"));
                        // mBgPaint.setColor(Color.YELLOW);
                        canvas.drawLine(x, 0, x, bgHeight, mBgPaint);
                        break;
                }
            }
        }
        
    }
    
    private void drawLine(int i, int startNum, float startY, ArrayList<Integer> lineArray,
            float[] widthArray, Canvas canvas) {
        
        if (lineArray == null || i >= lineArray.size()) {
            return;
        }
        int beforePoint = lineArray.get(i - 1) + startNum - 1;
        int end = lineArray.get(i) + startNum - 1;
        
        float beginX = widthArray[beforePoint - 1] + mWidthList.get(beforePoint).intValue() / 2f;
        float beginY = startY - itemHeight / 2f;
        float endX = widthArray[end - 1] + mWidthList.get(end).intValue() / 2f;
        float endY = startY + itemHeight / 2f;
        
        canvas.drawLine(beginX, beginY, endX, endY, mLinePaint);
    }
}
